package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import csu.web.credit_bank.mapper.CreditRecordsMapper;
import csu.web.credit_bank.mapper.CreditSummaryMapper;
import csu.web.credit_bank.pojo.CreditRecord;
import csu.web.credit_bank.pojo.CreditSummary;
import csu.web.credit_bank.service.CreditRecordsService;
import csu.web.credit_bank.service.CreditSummaryService;
import csu.web.credit_bank.utils.Result;
import csu.web.credit_bank.utils.UUIDManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Service
public class CreditRecordsServiceImpl extends ServiceImpl<CreditRecordsMapper, CreditRecord> implements CreditRecordsService {

    @Autowired
    private UUIDManager uuidManager;
    @Autowired
    private CreditRecordsMapper creditRecordsMapper;
   @Autowired
   private CreditSummaryMapper creditSummaryMapper;

    //硬编码古诗词
    private static final Map<String, List<String>> POEM_CORRECT_ANSWERS = new HashMap<>();

    static {
        //古诗词填空答案
        POEM_CORRECT_ANSWERS.put("poem_001", Arrays.asList("疑是地上霜", "低头思故乡")); // 静夜思
        POEM_CORRECT_ANSWERS.put("poem_002", Arrays.asList("黄河入海流", "更上一层楼")); // 登鹳雀楼
        POEM_CORRECT_ANSWERS.put("poem_003", Arrays.asList("树阴照水爱晴柔", "早有蜻蜓立上头")); // 小池，注意这里答案顺序要匹配前端填空顺序
    }

    @Override
    public CreditRecord createCreditRecord(CreditRecord creditRecord) {
        String id = uuidManager.getUUID();
        if (id == null) {
            throw new RuntimeException("ID池已空，无法创建学分记录");
        }

        creditRecord.setId(id);
        creditRecordsMapper.insert(creditRecord);
        // 更新学分汇总表
        CreditSummary creditSummary = creditSummaryMapper.selectOne(new QueryWrapper<CreditSummary>().eq("user_id", creditRecord.getUserId()));

        if (creditSummary == null) {
            // 如果不存在，则创建新的记录
            creditSummary = new CreditSummary();
            creditSummary.setUserId(creditRecord.getUserId());
            creditSummary.setTotalCredits(creditRecord.getCredits());
            creditSummary.setCurrentCredits(creditRecord.getCredits());
            creditSummary.setLastUpdateTime(new  Date());
            creditSummaryMapper.insert(creditSummary);
        } else {
            // 如果存在，则更新总学分
            if(creditRecord.getCredits()>0) creditSummary.setTotalCredits(creditSummary.getTotalCredits() + creditRecord.getCredits());
            creditSummary.setCurrentCredits(creditSummary.getCurrentCredits() + creditRecord.getCredits());
            creditSummary.setLastUpdateTime(new Date());
            creditSummaryMapper.updateById(creditSummary);
        }

        return creditRecord;
    }

    @Override
    public List<CreditRecord> getCreditRecordById(String id) {
        return creditRecordsMapper.selectList(new QueryWrapper<CreditRecord>().eq("user_id", id));
    }

    @Override
    public CreditRecord updateCreditRecord(CreditRecord creditRecord) {
        this.updateById(creditRecord);
        return this.getById(creditRecord.getId());
    }

    @Override
    public boolean deleteCreditRecord(String id) {
        return this.removeById(id);
    }


    public boolean hasStudentReceivedCourseCredit(String userId, String courseName) {
        QueryWrapper<CreditRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("course_name", courseName);
        queryWrapper.eq("method", "课程学习"); // 假设课程学分认证通过后，记录的 method 是“课程学习”
        queryWrapper.gt("credits", 0); // 确保是获得了学分 (credits > 0)

        // 查询符合条件的记录数量，如果大于0则表示已获得
        return creditRecordsMapper.selectCount(queryWrapper) > 0;
    }
}